package jf.jplayground.store;

import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.record.impl.ODocument;

public class DocumentDataStore {

    public static void main(String[] args) {
        DocumentDataStore store = new DocumentDataStore();

        store.demo();
    }

    public void demo() {

        ODatabaseDocumentTx db = openDataStore();

        // createDocument(db);

        for (ODocument doc : db.browseCluster("Person")) {
            System.out.println(doc);

        }

        db.close();

    }

    private void createDocument(ODatabaseDocumentTx db) {
        // CREATE A NEW DOCUMENT AND FILL IT
        ODocument doc = new ODocument(db, "Person");
        doc.field("name", "Luke");
        doc.field("surname", "Skywalker");
        doc.field("city", new ODocument(db, "City").field("name", "Rome").field("country", "Italy"));

        // SAVE THE DOCUMENT
        doc.save();
    }

    private ODatabaseDocumentTx openDataStore() {
        // // OPEN THE DATABASE
        // ODatabaseDocumentTx db = new
        // ODatabaseDocumentTx("remote:localhost/petshop").open("admin",
        // "admin");
        ODatabaseDocumentTx db = new ODatabaseDocumentTx("local:/tmp/databases/petshop");// .create();
        if (db.exists() == false) {
            db.create();
        } else {
            db.open("admin", "admin");
        }
        
        return db;
    }
}
